package net.lulihu.pangolin.headless.client.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import net.lulihu.pangolin.headless.common.container.annotation.Component;
import net.lulihu.pangolin.headless.common.protocol.ProtocolMessage;
import net.lulihu.pangolin.headless.common.protocol.ProtocolMessageType;
import net.lulihu.pangolin.headless.common.protocol.handler.RegistrationMessageTypeHandlerComponent;
import net.lulihu.pangolin.headless.common.util.ChannelTwoWayBindKit;

/**
 * 服务器用户频道丢失绑定消息处理程序
 */
@Slf4j
@Component
public class ServerUserChannelLostBindMessageHandler implements RegistrationMessageTypeHandlerComponent {

    @Override
    public byte messageType() {
        return ProtocolMessageType.S_USER_CHANNEL_LOST_BINDING;
    }

    @Override
    public void handle(ChannelHandlerContext context, ProtocolMessage message) {
        if (log.isDebugEnabled()) log.debug("收到服务端用户频道丢失绑定消息");

        Channel proxyChannel = context.channel();
        Channel userChannel = ChannelTwoWayBindKit.getBindChannel(proxyChannel);

        // 关闭用户通道
        userChannel.close();
        // 取消绑定
        ChannelTwoWayBindKit.unbind(proxyChannel, userChannel);
    }

}
